"""
用户管理后台配置
"""

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.html import format_html
from .models import User, UserProfile, UserAddress, Blacklist


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    """
    用户管理
    """
    
    list_display = [
        'username', 'email', 'nickname', 'role', 'is_active', 
        'is_verified', 'date_joined', 'get_avatar'
    ]
    
    list_filter = [
        'role', 'is_active', 'is_verified', 'gender', 
        'date_joined', 'last_login'
    ]
    
    search_fields = [
        'username', 'email', 'nickname', 'phone', 
        'student_id', 'major'
    ]
    
    ordering = ['-date_joined']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('username', 'password', 'email', 'nickname', 'avatar')
        }),
        ('个人信息', {
            'fields': ('phone', 'gender', 'birthday')
        }),
        ('学校信息', {
            'fields': ('student_id', 'major', 'grade', 'dormitory')
        }),
        ('权限', {
            'fields': ('role', 'is_active', 'is_staff', 'is_superuser', 'is_verified')
        }),
        ('重要日期', {
            'fields': ('last_login', 'date_joined')
        }),
        ('组和权限', {
            'fields': ('groups', 'user_permissions'),
        }),
    )
    
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'email', 'password1', 'password2', 'role'),
        }),
    )
    
    def get_avatar(self, obj):
        """显示头像"""
        if obj.avatar:
            return format_html(
                '<img src="{}" width="30" height="30" style="border-radius: 50%;" />',
                obj.avatar.url
            )
        return "无头像"
    get_avatar.short_description = '头像'
    
    def get_queryset(self, request):
        """优化查询"""
        return super().get_queryset(request).select_related('profile')


@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    """
    用户详细信息管理
    """
    
    list_display = [
        'user', 'credit_score', 'balance', 'post_count', 
        'comment_count', 'like_count'
    ]
    
    list_filter = ['credit_score']
    
    search_fields = ['user__username', 'user__nickname', 'bio']
    
    readonly_fields = ['credit_score', 'post_count', 'comment_count', 'like_count']
    
    fieldsets = (
        ('用户信息', {
            'fields': ('user',)
        }),
        ('个人简介', {
            'fields': ('bio', 'interests')
        }),
        ('统计数据', {
            'fields': ('credit_score', 'balance', 'post_count', 'comment_count', 'like_count')
        }),
    )


@admin.register(UserAddress)
class UserAddressAdmin(admin.ModelAdmin):
    """
    用户地址管理
    """
    
    list_display = [
        'user', 'receiver_name', 'receiver_phone', 
        'address', 'is_default', 'created_at'
    ]
    
    list_filter = ['is_default', 'created_at']
    
    search_fields = [
        'user__username', 'receiver_name', 'receiver_phone', 'address'
    ]
    
    ordering = ['-created_at']
    
    fieldsets = (
        ('用户信息', {
            'fields': ('user',)
        }),
        ('收货信息', {
            'fields': ('receiver_name', 'receiver_phone', 'address')
        }),
        ('地址设置', {
            'fields': ('is_default',)
        }),
    )


@admin.register(Blacklist)
class BlacklistAdmin(admin.ModelAdmin):
    """
    黑名单管理
    """
    
    list_display = [
        'user', 'reason', 'admin_user', 'created_at', 'expires_at'
    ]
    
    list_filter = ['created_at', 'expires_at']
    
    search_fields = ['user__username', 'reason', 'admin_user__username']
    
    ordering = ['-created_at']
    
    fieldsets = (
        ('用户信息', {
            'fields': ('user',)
        }),
        ('黑名单信息', {
            'fields': ('reason', 'admin_user')
        }),
        ('时间信息', {
            'fields': ('created_at', 'expires_at')
        }),
    )
    
    readonly_fields = ['created_at']
